<?php
class YiichenkuoController extends Controller
{
	/**
	 * 
	 * @see CController::filters()
	 * 过滤所有动作
	 * 在accessrules中设置过滤动作规则
	 */
	private $Identity;
	public function filters()
	{
		return array(
				'accessControl');
	}
	public function accessRules_1()
	{
		return array(
            array('allow',
                'actions'=>array('index',),
                'roles'=>array('*'),
            ),
			array('allow',
					'action'=>array('login','reg')),
					'role'=>array('?'),
			array('allow',
				'actions'=>array('logout'),
				'role'=>array('@'),
					),	
            array('deny',
                'actions'=>array('login','reg'),
                'users'=>array('@'),
            		),
				);
	}
	public function accessRules()
	{
		return array();
	}
	/**
	 * 默认动作首页
	 */
	public function actionIndex()
	{
		$array=array();
		$criteria=new CDbCriteria();
		$criteria->select='shopid,shopname,shopdescription';
		$criteria->limit=10;		
		$shop=Shop::model()->findAll();
		$result=array();
		foreach ($shop as $key=>$value)
		{
			$array=array();
			foreach ($value as $keys=>$values)
			{
				$array[$keys]=$values;
			}
			$result[]=$array;
		}
		$this->render('index',array('model'=>$result));
	}
	/**
	 * 注册动作
	 */
	public function actionReg()
	{
		
		$username=Yii::app()->request->getPost('username');
		$userpassword=Yii::app()->request->getPost('userpassword');
		if(!empty($username)&&!empty($userpassword))
		{
		$finduser=User::model()->find('username=:username',array(':username'=>$username));
		if($finduser==null)
		{
			$newuser=new User();
			$newuser->username=$username;
			$newuser->userpassword=$userpassword;
			$newuser->save();
			$this->redirect('index');
		}
		else 
			$this->render('reg');
		}
		else
			$this->render('reg');
	}
	/*
	 * 登陆动作
	 */
	public function actionLogin()
	{
		$username=Yii::app()->request->getPost('username');
		$userpassword=Yii::app()->request->getPost('userpassword');

		if(empty($username)||empty($userpassword))
			$this->render('login');
		else
		{
				$this->Identity=new YiichenkuoUserIdentity($username, $userpassword);
			if($this->Identity->authenticate())
			{
				Yii::app()->user->login($this->Identity,600);
				$this->redirect('index');
			}
			else 
				$this->render('login');
		}
		
	}
	/*
	 * 注销动作
	 */
	public function actionLogout()
	{
		yii::app()->user->logout();
		$this->redirect('index');
	}
	/**
	 * 购物车动作
	 */
	public function actionShopcar()
	{
		try{
		
		$username=Yii::app()->user->getName();
		$userinfo=User::model()->find("username=:username",array(':username'=>$username));
		
		$criteria=new CDbCriteria();
		$criteria->select='userid,shopid';	
		$criteria->addCondition('userid='.$userinfo['userid']);
		$buyshop=Buyshop::model()->findAll($criteria);
		$result=array();
		foreach($buyshop as $key=>$value)
		{
			$tempcriteria=new CDbCriteria();
			$tempcriteria->select='shopid,shopname,shopdescription';
			$tempcriteria->addCondition('shopid='.$value['shopid']);
			$tempresult=shop::model()->find($tempcriteria);
			$array=array();
			$array['shopid']=$tempresult['shopid'];
			$array['shopname']=$tempresult['shopname'];
			$array['shopdescription']=$tempresult['shopdescription'];
			$result[]=$array;
			
		}
		
		}catch (Exception $e)
		{
			echo $e->getMessage();
			echo $e->getCode();
			echo $e->getTraceAsString();
		}
		$result['pagesize']=10;
		print_r($result);
		$this->render('shopcar',array('model'=>$result));
	}
	public function actionAddshopcar()
	{
		try{
		if(Yii::app()->user->isGuest)
		{
			echo 2;
			return 2;
		}
		$shopid=Yii::app()->request->getPost('shopid');
		$user=User::model()->find("username=:username",array('username'=>Yii::app()->user->getName()));
		$shopcar=new Buyshop();
		$shopcar->userid=$user['userid'];
		$shopcar->shopid=$shopid;
		$shopcar->save();
		echo 1;
		}catch(Exception $e)
		{
			echo 3;
		}
	}
	public function actionTestextract()
	{
		$array=array();
		$array["name"]=array("firstname"=>"王","secoundname"=>"东明");
		extract($array);
		$this->render('testextract',$array);
	}
	
	
	
	
}












